import { ADD_COUNTER, ADD_TO_CART, ADD_TO_SC, SUB_TO_SC, ACT_TRUE, DL_XG } from './mutations-type.js'
export default {
  // 将商品加入购物车的异步方法
  addCart(context, payload) {
    return new Promise((resolve, reject) => {
      const oldProduct = context.state.cartlist.find(item => item.iid === payload.iid)
      // 2. 判断oldProduct是有值
      if (oldProduct) { // 数量加一
        context.commit(ADD_COUNTER, oldProduct)
        resolve("当前商品数量+1")
      } else { // 添加新的商品到购物车
        context.commit(ADD_TO_CART, payload)
        resolve("添加成功，在购物车等亲~")
      }
    })
  },
  // 收藏商品的异步方法
  scList(context, payload) {
    return new Promise((resolve, reject) => {
      const oldProduct = context.state.sclist.find(item => item.iid === payload.iid)
      context.commit(ACT_TRUE, payload)
      if (oldProduct) {
        context.commit(SUB_TO_SC, payload)
        resolve('取消收藏')
      } else {
        context.commit(ADD_TO_SC, payload)
        resolve('收藏成功')
      }
    })
  },
  // 判断登录信息
  dl_xg(context) {
    context.commit(DL_XG)
  }
}
